package mapper

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	context2 "golang.org/x/net/context"
	"ksd-social-api/modules/note/model"
)

type NoteChapterMapper struct {
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteChapterMapper) FindChapterLessonsStatus(noteId uint64, systemId uint64) ([]*model.NoteChapter, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	// 1：准备容器对象，开始装载数据库数据
	noteChapterList := []*model.NoteChapter{}
	_, err := db.Raw("select id,title,sorted,status,total_time,note_id,create_time,update_time,free_flag,videoid,duration,videosize,video_source_url from xk_note_chapter where note_id = ? and system_id = ? and status = 1 and is_deleted = 0 order by sorted asc").SetArgs(noteId, systemId).QueryRows(&noteChapterList)
	// 4: 结构返回
	return noteChapterList, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteChapterMapper) FindChapterLessons(noteId uint64, systemId uint64) ([]*model.NoteChapter, error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	// 1：准备容器对象，开始装载数据库数据
	noteChapterList := []*model.NoteChapter{}
	_, err := db.Raw("select id,title,sorted,status,total_time,note_id,create_time,update_time,free_flag,videoid,duration,videosize,video_source_url from xk_note_chapter where note_id = ? and system_id = ? and is_deleted = 0 order by sorted asc").SetArgs(noteId, systemId).QueryRows(&noteChapterList)
	// 4: 结构返回
	return noteChapterList, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteChapterMapper) RemoveById(id uint64, noteId uint64, systemId uint64) (flag bool) {
	// 创建orm对象
	err := orm.NewOrm().DoTx(func(ctx context2.Context, txOrm orm.TxOrmer) (err error) {
		dbChapter := model.NoteChapter{Id: id, NoteId: noteId, SystemId: systemId}
		_, err = txOrm.Delete(&dbChapter)
		_, err = txOrm.Raw("delete from xk_note_lesson  where chapter_id = ? and note_id = ? and system_id = ?", id, noteId, systemId).Exec()
		return err
	})
	return nil == err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id查询小册信息
 */
func (mapper *NoteChapterMapper) GetById(id uint64, systemId uint64) (noteChapter *model.NoteChapter, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 2: 赋予id给结构体
	dbChapter := model.NoteChapter{Id: id, SystemId: systemId}
	// 3: 读取id的数据放入到结构体，进行返回
	err = db.Read(&dbChapter, "id", "system_id")
	// 4: 结构返回
	return &dbChapter, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 保存小册章信息
 */
func (mapper *NoteChapterMapper) SaveChapter(noteChapter *model.NoteChapter) (chapter *model.NoteChapter, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	_, err = db.Insert(noteChapter)
	// 4: 结构返回
	return noteChapter, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改小册信息
 */
func (mapper *NoteChapterMapper) UpdateById(field string, value any, id uint64) (flag bool, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	exec, err := db.Raw(fmt.Sprintf("update xk_note_chapter set %s=?,update_time = now() where id = ?", field)).SetArgs(value, id).Exec()
	// 4: 结构返回
	affected, _ := exec.RowsAffected()
	return affected > 0, err
}

/**
 * @author feige
 * @date 2023-10-08
 * @desc 根据id修改小册信息
 */
func (mapper *NoteChapterMapper) UpdateByIdContent(mdcontent string, htmlcontent string,
	systemId uint64, noteId uint64, id uint64) (flag bool, err error) {
	// 1: 创建一个数据连接
	db := orm.NewOrm()
	// 3: 读取id的数据放入到结构体，进行返回
	exec, err := db.Raw("update xk_note_chapter set mdcontent=?,htmlcontent=?,update_time = now() where id = ? and note_id = ? and system_id = ?").SetArgs(mdcontent, htmlcontent, id, noteId, systemId).Exec()
	db.Raw("update xk_note set update_time = now() where id = ? and system_id = ?").SetArgs(noteId, systemId).Exec()
	// 4: 结构返回
	affected, _ := exec.RowsAffected()
	return affected > 0, err
}
